package com.hll.api.config.web;

import com.alibaba.fastjson.JSON;

/**
 * 接口统一返回类
 *
 * @author 何龙
 * @create 2019-06-02 10:28
 */
public class RespMessage {
    public enum Status {
        /**
         * 请求成功
         */
        OK( 0 ),

        /**
         * 业务失败
         */
        ERROR( 2 ),

        /**
         * 没有权限
         */
        NON_PERMISSION( 101 ),

        /**
         * 未知错误
         */
        UNKNOWN_ERR( -1 );

        private Integer status;

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

        Status(Integer status) {
            this.status = status;
        }
    }

    private Integer status;

    private String err = "";

    private Object datas;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = err;
    }

    public Object getDatas() {
        return datas;
    }

    public void setDatas(Object datas) {
        this.datas = datas;
    }

    private RespMessage() {
        this.status = Status.OK.status;
    }

    private RespMessage(Integer status, String err, Object datas) {
        super();
        this.status = status;
        this.err = err;
        this.datas = datas;
    }

    public static RespMessage success() {
        return RespMessage.builder().setStatus( Status.OK ).setErr( Message.SUCCESS ).build();
    }

    public static RespMessage success(String msg) {
        return RespMessage.builder().setStatus( Status.OK ).setErr( msg ).build();
    }

    public static RespMessage success(Object datas, String msg, Object... args) {
        return RespMessage.builder().setStatus( Status.OK ).setErr( msg, args ).setDatas( datas ).build();
    }

    public static RespMessage error(String msg, Object... args) {
        return RespMessage.builder().setStatus( Status.ERROR ).setErr( msg, args ).build();
    }

    public static RespMessage loginError() {
        return RespMessage.builder().setStatus( Status.NON_PERMISSION ).setErr( "未登录" ).build();
    }

    public static RespMessage error(Status status, String msg, Object... args) {
        return RespMessage.builder().setStatus( status ).setErr( msg, args ).build();
    }

    public static RespMessageBuilder builder() {
        return new RespMessageBuilder();
    }

    public static class RespMessageBuilder {
        Status status;
        String err = "";
        Object datas;

        public RespMessage build() {
            assert status != null;
            return new RespMessage( status.status, err, datas );
        }

        public RespMessageBuilder success() {
            this.status = Status.OK;
            this.setErr( Message.SUCCESS );
            return this;
        }

        public RespMessageBuilder error() {
            this.status = Status.ERROR;
            this.setErr( Message.ERR );
            return this;
        }


        public RespMessageBuilder setStatus(Status status) {
            this.status = status;
            return this;
        }

        public RespMessageBuilder setErr(String err, Object... args) {
            this.err = err;
            this.datas = args;
            return this;
        }

        public RespMessageBuilder setDatas(Object datas) {
            this.datas = datas;
            return this;
        }

    }

    @Override
    public String toString() {
        return JSON.toJSONString( this );
    }
}
